home *** CD-ROM | disk | FTP | other *** search
- Imports System.Runtime.Serialization
-
- ' a serializable Person class
-
- <Serializable()> _
- Class Person
- Public FirstName As String
- Public LastName As String
- Private BirthDate As Date
-
- ' don't serialize this field, which can be easily recalculated
- <NonSerialized()> Private m_Age As Integer
-
- ' this field might cause circular references.
- Public Spouse As Person
-
- ' Note that BirthDate can be set only through the constructor method.
- Sub New(ByVal FirstName As String, ByVal LastName As String, _
- ByVal BirthDate As Date)
- Me.FirstName = FirstName
- Me.LastName = LastName
- Me.BirthDate = BirthDate
- End Sub
-
- ' The Age property caches its value in the m_Age private variable.
-
- ReadOnly Property Age() As Integer
- Get
- ' Evaluate the Age if not cached already.
- If m_Age = 0 Then m_Age = Year(Now) - Year(BirthDate)
- Return m_Age
- End Get
- End Property
- End Class
-
- ' a class that supports custom serialization
-
- <Serializable()> _
- Class CompactDoubleArray
- Inherits ArrayList
- Implements ISerializable
-
- ' Elements that have this value aren't persisted.
- Public DefaultValue As Double
-
- ' We need this default constructor, otherwise the class
- ' couldn't be instantiated from regular clients.
- Sub New()
- MyBase.New()
- End Sub
-
- ' The special constructor implied by ISerializable.
- Private Sub New(ByVal info As SerializationInfo, _
- ByVal context As StreamingContext)
-
- ' Create the base ArrayList object.
- MyBase.New()
- ' Retrieve DefaultValue.
- DefaultValue = info.GetDouble("DefaultValue")
- ' Retrieve number of elements.
- Dim elCount As Integer = info.GetInt32("Count")
-
- Dim index As Integer, Value As Double
- For index = 0 To elCount - 1
- Try
- ' Try to assign the value in the SerializationInfo.
- Value = info.GetDouble(index.ToString)
- Catch
- ' If SerializeInfo doesn't contain that value,
- ' use the default value.
- Value = DefaultValue
- End Try
- ' Add the value to the inner ArrayList.
- MyBase.Add(Value)
- Next
- End Sub
-
- ' Serialize this object.
- Sub GetObjectData(ByVal info As SerializationInfo, _
- ByVal context As StreamingContext) _
- Implements ISerializable.GetObjectData
-
- ' Remember DefaultValue.
- info.AddValue("DefaultValue", DefaultValue)
- ' Remember the total number of elements.
- info.AddValue("Count", MyBase.Count)
-
- ' Serialize only elements whose value is different from DefaultValue.
- Dim index As Integer, Value As Double
- For index = 0 To MyBase.Count - 1
- ' The AddValue method requires a specific type.
- Value = CType(MyBase.Item(index), Double)
- If Value <> DefaultValue Then
- ' Store only if different from default value.
- info.AddValue(index.ToString, Value)
- End If
- Next
- End Sub
- End Class
-
- ' a modified version of the class, that compacts data only when saving
- ' to a file or a remote machine
-
- <Serializable()> _
- Class CompactDoubleArray2
- Inherits ArrayList
- Implements ISerializable
-
- ' Elements that have this value aren't persisted.
- Public DefaultValue As Double
-
- ' We need this default constructor, otherwise the class
- ' couldn't be instantiated from regular clients.
- Sub New()
- MyBase.New()
- End Sub
-
- ' The special constructor implied by ISerializable.
- Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
- ' Create the base ArrayList object.
- MyBase.New()
-
- ' Attempt to get the serialized ArrayList.
- Try
- ' Try to read an element named ArrayList.
- Dim al As ArrayList
- al = DirectCast(info.GetValue("ArrayList", GetType(ArrayList)), ArrayList)
- ' If all went well, add to inner ArrayList and exit.
- MyBase.AddRange(al)
- Exit Sub
- Catch
- ' if failed, continue to extract elements.
- End Try
-
- ' Retrieve DefaultValue.
- DefaultValue = info.GetDouble("DefaultValue")
- ' Retrieve number of elements.
- Dim elCount As Integer = info.GetInt32("Count")
-
- Dim index As Integer, Value As Double
- For index = 0 To elCount - 1
- Try
- ' Try to assign the value in the SerializationInfo.
- Value = info.GetDouble(index.ToString)
- Catch
- ' If SerializeInfo doesn't contain that value,
- ' use the default value.
- Value = DefaultValue
- End Try
- ' Add the value to the inner ArrayList.
- MyBase.Add(Value)
- Next
- End Sub
-
- ' Serialize this object.
- Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) _
- Implements ISerializable.GetObjectData
-
- ' If not serializing to a file or a remote machine,
- ' then use non non-compact format.
- If context.State <> StreamingContextStates.File And context.State <> StreamingContextStates.CrossMachine Then
- ' Serialize the inner ArrayList and exit.
- info.AddValue("ArrayList", MyBase.Clone)
- Exit Sub
- End If
-
- ' Remember DefaultValue.
- info.AddValue("DefaultValue", DefaultValue)
- ' Remember the total number of elements.
- info.AddValue("Count", MyBase.Count)
-
- ' Serialize only elements whose value is different from DefaultValue.
- Dim index As Integer, Value As Double
- For index = 0 To MyBase.Count - 1
- ' The AddValue method requires a specific type.
- Value = CType(MyBase.Item(index), Double)
- If Value <> DefaultValue Then
- ' Store only if different from default value.
- info.AddValue(index.ToString, Value)
- End If
- Next
- End Sub
- End Class
-
- ' A very simple serializable class
- <Serializable()> Class Point
- Public X As Single
- Public Y As Single
-
- Sub New(ByVal X As Single, ByVal Y As Single)
- Me.X = X
- Me.Y = Y
- End Sub
-
- Overrides Function ToString() As String
- Return "(" & X.ToString & "," & Y.ToString & ")"
- End Function
- End Class
-
- ' An array of Points that can serialize in a compact format
- ' provides an example of how to use IDeserializationCallback
-
- <Serializable()> _
- Class CompactPointArray
- Inherits ArrayList
- Implements ISerializable, IDeserializationCallback
-
- ' This variable cashes the object passed to the constructor.
- Dim m_info As SerializationInfo
-
- ' We need this constructor.
- Sub New()
- MyBase.New()
- End Sub
-
- ' The special constructor implied by ISerializable.
- Private Sub New(ByVal info As SerializationInfo, _
- ByVal context As StreamingContext)
- ' Create an ArrayList object.
- MyBase.New()
- ' Cache the SerializationInfo object.
- m_info = info
- End Sub
-
- ' This method is called when the object graph has been
- ' completely deserialized.
- Sub OnDeserialization(ByVal sender As Object) _
- Implements IDeserializationCallback.OnDeserialization
- ' Retrieve number of elements .
- Dim elCount As Integer = m_info.GetInt32("Count")
-
- Dim index As Integer, p As Point
- For index = 0 To elCount - 1
- Try
- ' Try to assign the value in the SerializationInfo.
- p = CType(m_info.GetValue(index.ToString, GetType(Point)), Point)
- Catch
- ' if any error, use a default "null" Point object
- p = New Point(0, 0)
- End Try
- ' Add to the inner ArrayList.
- MyBase.Add(p)
- Next
- ' We don't need this object any longer.
- m_info = Nothing
- End Sub
-
- ' Serialize only non-Nothing elements.
- Sub GetObjectData(ByVal info As SerializationInfo, ByVal _
- context As StreamingContext) Implements ISerializable.GetObjectData
-
- ' Remember the total number of elements.
- info.AddValue("Count", MyBase.Count)
-
- ' Serialize only elements whose value is different from (0,0).
- Dim index As Integer, p As Point
- For index = 0 To MyBase.Count - 1
- ' The AddValue method requires a specific type.
- p = DirectCast(MyBase.Item(index), Point)
- If p.X <> 0 Or p.Y <> 0 Then
- ' Add to SerializationInfo only if <> (0,0).
- info.AddValue(index.ToString, p)
- End If
- Next
- End Sub
- End Class
-